﻿/*
Usage :
var my_menu:MyContextMenu = new MyContextMenu(this); // any Display Object.
my_menu.init();
my_menu.update(); // remember to update to take effect of change.



*/

package com.cf.core.model{
	import flash.display.*;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuBuiltInItems;
	import flash.ui.ContextMenuClipboardItems;
	import flash.ui.ContextMenuItem;
	import flash.events.ContextMenuEvent;
	import com.cf.core.model.MyContextList;
	public class MyContextMenu extends Sprite{
		public static const LINE:String = "SeparatorLine";
		public static const ERRORITEM:ContextMenuItem = new ContextMenuItem("ERROR");
		private var G_target:*;
		private var G_menu:ContextMenu;
		private var G_list:Array;
		/*
		public function set list(_a:MyContextList):void{G_list=_a}
		public function get list():MyContextList{return G_list}
		//*/
		private var G_autoUpdate:Boolean;
		public function set auto(_t:Boolean):void{G_autoUpdate=_t}
		public function get auto():Boolean{return G_autoUpdate}
		private var G_buildInFn:Object;
		
		
		public function MyContextMenu(_stage:*):void{
			G_target=_stage;
			G_list=new Array();
			G_buildInFn=new Object();
			G_autoUpdate=false;
			init(false);
		}//fn
		public function init(_hideBuiltIn:Boolean=true):void{
			G_menu = new ContextMenu();
			if( _hideBuiltIn ){
				G_menu.hideBuiltInItems();
				G_menu.builtInItems.forwardAndBack = false;
				G_menu.builtInItems.loop = false;
				G_menu.builtInItems.play = false;
				G_menu.builtInItems.print = false;
				G_menu.builtInItems.quality = false;
				G_menu.builtInItems.rewind = false;
				G_menu.builtInItems.save = false;
				G_menu.builtInItems.zoom = false;
			}//if
		}//fn
		public function addButton(_onTxt:String,_offTxt:String,_fn:Function=null,_fn2:Function=null):void{
			var _on:ContextMenuItem = new ContextMenuItem(_onTxt,false,true,true);
			var _off:ContextMenuItem = new ContextMenuItem(_offTxt,false,true,false);
			var _listno:Number = G_list.length;
			_on.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleListButton);
			_off.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleListButton);
			G_buildInFn["itemNo"+_listno] = {btn:(_listno+1), fn:_fn}
			G_buildInFn["itemNo"+(_listno+1)] = {btn: (_listno), fn:_fn2}
			G_list.push(_on);
			G_list.push(_off);
		}//fn
		private function handleListButton(e:ContextMenuEvent):void{
			var _item:ContextMenuItem = getItem( e.target );
			if(_item===ERRORITEM){
				// Error
				trace("MyContextMenu : e.target Fail ["+ (_item===ERRORITEM) +"]");
			}else{
				runItemFn(_item);
			}//if
		}//fn
		private function getItem(_o:*):ContextMenuItem{
			try{
				for(var i:int=0;i<G_list.length;i++){
					if(G_list[i] === _o) return G_list[i];
				}//for
			}catch(e){ return ERRORITEM; }
			return ERRORITEM;
		}//fn
		private function runItemFn(_o:*):void{
			try{
				for(var i:int=0;i<G_list.length;i++){
					if(G_list[i] === _o){
						// trace("no = "+ G_buildInFn["itemNo"+i].btn+" && btn = "+ G_list[G_buildInFn["itemNo"+i].btn] +" total = "+ G_list.length);
						G_list[i].visible = false;
						G_list[G_buildInFn["itemNo"+i].btn].visible = true;
						if( G_buildInFn["itemNo"+i].fn!=null ) G_buildInFn["itemNo"+i].fn();
						// Fix when there is .separatorBefore = true;
						G_list[G_buildInFn["itemNo"+i].btn].separatorBefore = G_list[i].separatorBefore;
					}//if
				}//for
			}catch(e){}
		}//fn
		public function addLabel(_str:String,_fn:Function=null):void{
			var _MI:ContextMenuItem = new ContextMenuItem(_str,false,true,true); // text, line, enable, visible
			if(_fn!=null){
				_MI.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, _fn);
			}//if
			G_list.push(_MI);
		}//fn
		public function addLine():void{
			G_list.push(LINE);
		}//fn
		public function update():void{
			for(var i:int=0;i<G_list.length;i++){
				if( G_list[i]==LINE ){
					// Separator Line.
					if( i!=(G_list.length-1) ){
						// not the last one, and the next one must be normal Label.
						try{
							G_list[i+1].separatorBefore = true;
						}catch(e){}
					}//if
				}else{
					// Normal Item
					G_menu.customItems.push(G_list[i]);
				}//if
			}//for
			G_target.contextMenu = G_menu;
		}//fn
		public function RELEASE():void{
			init(true);
			G_list=new Array();
			G_buildInFn=new Object();
			G_autoUpdate=false;
			update();
		}//fn
	}//class
}//package